/*
 * Copyright (c) 2020, 2025, keyila All rights reserved.
 *
 */
package com.clp.study.controller;

import com.clp.bean.Result;
import com.clp.study.convert.UserDtoConvert;
import com.clp.study.entity.dto.UserDto;
import com.clp.study.entity.req.PageUserReq;
import com.clp.study.entity.req.UserReq;
import com.clp.study.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author clp
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户Controller")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户",notes = "用户数据")
    public Result addUser(@RequestBody UserReq userReq){
        UserDto userDto = UserDtoConvert.INSTANCE.coReqToUserDto(userReq);
        return Result.ok(userService.addOne(userDto));
    }

    @DeleteMapping("/{id}")
    @Cacheable(cacheNames = "userXCon",key = "'dalian'+#id")
    public Result deleteOneUser(@PathVariable Integer id){
        return Result.ok(userService.deleteOneUser(id));
    }

    @GetMapping("/getPage")
    public Result getPageUser(@RequestBody PageUserReq pageUserReq){
        UserDto userDto=new UserDto();
        BeanUtils.copyProperties(pageUserReq,userDto);
        return Result.ok(userService.getUserPage(userDto));
    }

}
